package com.cskaoyan.service.order;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.address.IdPo;
import com.cskaoyan.bean.order.*;

import com.cskaoyan.bean.order.bo.CreateOrderBo;

import com.cskaoyan.bean.order.bo.CommentBo;

import com.cskaoyan.bean.order.bo.OrderBo;
import com.cskaoyan.bean.order.bo.RefundBo;
import com.cskaoyan.bean.order.bo.ShipBo;
import com.cskaoyan.bean.order.vo.*;

import com.cskaoyan.mapper.*;

import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.OrderGoodsMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.OrderUserMapper;

import com.cskaoyan.service.AppletCouponService;
import com.cskaoyan.util.GetLogedUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import java.util.List;

/**
 * 创建日期: 2022/05/06 14:36
 *
 * @author JinLu Zhou
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    AppletCouponService appletCouponService;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    OrderUserMapper orderUserMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCartMapper marketCartMapper;
    @Autowired
    MarketGrouponRulesMapper marketGrouponRulesMapper;
    @Autowired
    MarketSystemMapper marketSystemMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    static Date birthday =new Date();
    @Override
    public InfoOrderVo list(PaginationPo paginationPo) {
        Integer userId = GetLogedUser.getUserId();
        List<ListEntity> list = getListEntities(userId);
        List<ListEntity> list1 = new LinkedList<>();
        Integer limit = paginationPo.getLimit();
        Integer page = paginationPo.getPage();
        Integer showType = paginationPo.getShowType();

        switch(showType) {
            case 0:
                list1=list;
                break;
            case 1:
                for (ListEntity listEntity : list) {
                  if(listEntity.getOrderStatusText().equals("未付款"))  {
                      list1.add(listEntity);
                  }
                }
                break;
            case 2:
                for (ListEntity listEntity : list) {
                    if(listEntity.getOrderStatusText().equals("已付款"))  {
                        list1.add(listEntity);
                    }
                }
                break;
            case 3:
                for (ListEntity listEntity : list) {
                    if(listEntity.getOrderStatusText().equals("已发货"))  {
                        list1.add(listEntity);
                    }
                }
                break;
            case 4:
                for (ListEntity listEntity : list) {
                    if(listEntity.getOrderStatusText().equals("用户收货"))  {
                        list1.add(listEntity);
                    }
                }
                break;
        }
        PageHelper.startPage(page, limit);
        PageInfo<ListEntity> pageInfo = new PageInfo<ListEntity>(list1);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return new InfoOrderVo(total,pages,limit,page,list1);
    }

    private List<ListEntity> getListEntities(Integer userId) {
        List<ListEntity> list=new ArrayList<>();

        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.setOrderByClause("id "+"desc");
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        for (MarketOrder marketOrder : marketOrders) {
           if(marketOrder.getUserId().equals(userId)&&!marketOrder.getDeleted()) {
               //marketOrder.getGrouponPrice()占时未用到
               ListEntity listEntity = new ListEntity(orderStatus(marketOrder).getString(),marketOrder.getAftersaleStatus(),false,marketOrder.getOrderSn(),marketOrder.getActualPrice(),marketOrder.getId());
               List<GoodsListEntity> goodsList=new ArrayList<>();
               List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(new MarketOrderGoodsExample());
               for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
                  if (marketOrderGood.getOrderId().equals(marketOrder.getId())){
                      goodsList.add(new GoodsListEntity(marketOrderGood.getNumber(),marketOrderGood.getPicUrl(),
                              marketOrderGood.getPrice(),marketOrderGood.getGoodsId(),marketOrderGood.getGoodsName(), getSpecList(marketOrderGood)));
                  }
               }
               listEntity.setHandleOption(orderStatus(marketOrder).getHandleOptionEntity());
               listEntity.setGoodsList(goodsList);
               list.add(listEntity);
           }
        }
        return list;
    }

    @Override
    public OrderGoodsDetailVo detail(Integer oderId) {
        OrderGoodsDetailVo orderGoodsDetailVo = new OrderGoodsDetailVo();
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(oderId);
        OrderInfoEntity orderInfoEntity =new OrderInfoEntity(marketOrder.getConsignee(),marketOrder.getAddress(),
                marketOrder.getAddTime(),marketOrder.getOrderSn(),marketOrder.getActualPrice(), marketOrder.getMobile(),
                marketOrder.getMessage(),orderStatus(marketOrder).getString(),marketOrder.getAftersaleStatus(),marketOrder.getGoodsPrice(),
                marketOrder.getCouponPrice(),marketOrder.getId(),marketOrder.getFreightPrice(),orderStatus(marketOrder).getHandleOptionEntity());
        List<OrderGoodsEntity> goodsEntityList=new ArrayList<>();
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(new MarketOrderGoodsExample());
        for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
         if(marketOrderGood.getOrderId().equals(oderId))   {
             OrderGoodsEntity orderGoodsEntity = new OrderGoodsEntity(marketOrderGood.getProductId(),marketOrderGood.getAddTime(),
                     marketOrderGood.getOrderId(),marketOrderGood.getGoodsId(),marketOrderGood.getGoodsSn(),marketOrderGood.getUpdateTime(),
                     marketOrderGood.getNumber(),marketOrderGood.getPicUrl(),marketOrderGood.getDeleted(),marketOrderGood.getPrice(),
                     marketOrderGood.getComment(),marketOrderGood.getId(),marketOrderGood.getGoodsName(),getSpecList(marketOrderGood));


             goodsEntityList.add(orderGoodsEntity);
         }
        }
        orderGoodsDetailVo.setExpressInfo(new LinkedList<>());
        orderGoodsDetailVo.setOrderGoods(goodsEntityList);
        orderGoodsDetailVo.setOrderInfo(orderInfoEntity);
        return orderGoodsDetailVo;
    }

    private List<String> getSpecList(MarketOrderGoods marketOrderGood) {
        List<String> stringList = new ArrayList<>();
        List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByExample(new MarketGoodsSpecificationExample());
        for (MarketGoodsSpecification marketGoodsSpecification : marketGoodsSpecifications) {
            if (marketGoodsSpecification.getGoodsId().equals(marketOrderGood.getGoodsId())) {
                stringList.add(marketGoodsSpecification.getValue());
            }
        }
        return stringList;
    }

    //8种状态判断
    private StatusVo orderStatus(MarketOrder marketOrder) {
        boolean comment=false;
        boolean aftersaleStatus=false;
        Short orderStatus = marketOrder.getOrderStatus();
        StatusVo statusVo = new StatusVo();
        Boolean deleted = marketOrder.getDeleted();
        switch(orderStatus)
        {
            case 101:
                statusVo.setString ("未付款");
                statusVo.setHandleOptionEntity(new HandleOptionEntity(true,false,false,true,comment,deleted,aftersaleStatus,false));
                break;
            case 102:
                statusVo.setString ("用户取消");
                statusVo.setHandleOptionEntity(new HandleOptionEntity(false,false,false,false,comment,deleted,aftersaleStatus,false));
                break;
            case 103:
                statusVo.setString ("系统取消");
                statusVo.setHandleOptionEntity(new HandleOptionEntity(false,false,false,false,comment,deleted,aftersaleStatus,false));
                break;
            case 201:
                statusVo.setString ("已付款");
                statusVo.setHandleOptionEntity(new HandleOptionEntity(false,false,false,false,comment,deleted,aftersaleStatus,true));
                break;
            case 202:
                statusVo.setString ("正在退款");
                statusVo.setHandleOptionEntity(new HandleOptionEntity(false,false,false,false,comment,deleted,aftersaleStatus,false));
                break;
            case 203:
                statusVo.setString ("已退款");
                statusVo.setHandleOptionEntity(new HandleOptionEntity(false,false,false,false,comment,deleted,aftersaleStatus,false));
                break;
            case 301:
                statusVo.setString ("已发货");
                statusVo.setHandleOptionEntity(new HandleOptionEntity(false,true,false,false,comment,deleted,aftersaleStatus,false));
                break;
            case 401:
                statusVo.setString ("用户收货");
                comment=true;
                statusVo.setHandleOptionEntity(new HandleOptionEntity(false,false,true,false,comment,true,true,false));
                break;
            case 402:
                statusVo.setString ("系统收货");

                statusVo.setHandleOptionEntity(new HandleOptionEntity(true,false,false,true,comment,true,true,false));
                break;

        }

        return statusVo;
    }





    @Override
    public BaseRespVo selectAllOrders(OrderBo orderBo) {
        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(orderBo.getSort() + " " + orderBo.getOrder());
        OrderExample.Criteria criteria = orderExample.createCriteria();
        //必要條件
        criteria.andDeletedEqualTo(false);
        if (orderBo.getStart() != null && !"".equals(orderBo.getStart())
                && orderBo.getEnd() != null && !"".equals(orderBo.getEnd())) {
            try {
                Date start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(orderBo.getStart());
                Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(orderBo.getEnd());
                criteria.andAddTimeBetween(start, end);
                criteria.andEndTimeBetween(start, end);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (orderBo.getOrderStatusArray() != null && orderBo.getOrderStatusArray().length != 0) {
            for (Integer integer : orderBo.getOrderStatusArray()) {
                criteria.andOrderStatusEqualTo((short) integer.intValue());
            }
        }
        if (orderBo.getUserId() != null) {
            criteria.andUserIdEqualTo(orderBo.getUserId());
        }
        if (orderBo.getOrderSn() != null) {
            criteria.andOrderSnEqualTo(orderBo.getOrderSn().toString());
        }
        PageHelper.startPage(orderBo.getPage(), orderBo.getLimit());
//执行查询过程中拼接分页信息
        List<Order> orders = orderMapper.selectByExample(orderExample);
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        long total = pageInfo.getTotal();
        int pages = (int) total / orderBo.getLimit() + 1;


        OrderVo<Order> orderOrderVo = new OrderVo<>();
        orderOrderVo.setLimit(orderBo.getLimit());
        orderOrderVo.setList(orders);
        orderOrderVo.setPage(orderBo.getPage());
        orderOrderVo.setTotal(Integer.valueOf((int) total));
        orderOrderVo.setPages(pages);

        BaseRespVo<OrderVo<Order>> orderVoBaseRespVo = new BaseRespVo<>();
        orderVoBaseRespVo.setErrmsg("成功");
        orderVoBaseRespVo.setErrno(0);
        orderVoBaseRespVo.setData(orderOrderVo);
        return orderVoBaseRespVo;
    }

    @Override
    public BaseRespVo selectOrdersDetail(Integer id) {

        OrderExample orderExample1 = new OrderExample();
        OrderExample.Criteria criteria = orderExample1.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIdEqualTo(id);
        //Order order = orderMapper.selectByExample(orderExample1).get(0);
        List<Order> orders = orderMapper.selectByExample(orderExample1);
        if (orders == null || orders.size() == 0) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(502);
            baseRespVo.setErrmsg("系统内部错误");
            return baseRespVo;
        }

        Order order = orderMapper.selectByExample(orderExample1).get(0);
        OrderDetailVo orderDetailVo = new OrderDetailVo();//
        orderDetailVo.set(order);

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria1 = orderGoodsExample.createCriteria();
        criteria1.andDeletedEqualTo(false);
        criteria1.andIdEqualTo(id);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        List<OrderGoodsVo> orderGoodsVos = new ArrayList<>();//
        for (OrderGoods orderGood : orderGoods) {
            OrderGoodsVo orderGoodsVo = new OrderGoodsVo();
            orderGoodsVo.set(orderGood);
            orderGoodsVos.add(orderGoodsVo);
        }
        UserVo userVo = new UserVo();//
        OrderUserExample orderUserExample = new OrderUserExample();
        OrderUserExample.Criteria criteria2 = orderUserExample.createCriteria();
        criteria2.andDeletedEqualTo(false);
        criteria2.andIdEqualTo(order.getUserId());
        OrderUser orderUser = orderUserMapper.selectByExample(orderUserExample).get(0);
        userVo.setNickname(orderUser.getNickname());
        userVo.setAvatar(orderUser.getAvatar());

        OrderGoodsDataVo orderGoodsDataVo = new OrderGoodsDataVo();

        orderGoodsDataVo.setOrder(orderDetailVo);
        orderGoodsDataVo.setOrderGoods(orderGoodsVos);
        orderGoodsDataVo.setUser(userVo);

        BaseRespVo<OrderGoodsDataVo> orderGoodsDataVoBaseRespVo = new BaseRespVo<>();
        orderGoodsDataVoBaseRespVo.setData(orderGoodsDataVo);
        orderGoodsDataVoBaseRespVo.setErrno(0);
        orderGoodsDataVoBaseRespVo.setErrmsg("成功");
        return orderGoodsDataVoBaseRespVo;
    }

    @Override
    public BaseRespVo deleteOrderById(Integer orderId) {
        Order order = new Order();
        order.setDeleted(true);
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(orderId);
        orderMapper.updateByExampleSelective(order, orderExample);

        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    @Override
    public BaseRespVo ship(ShipBo shipBo) {
        Order order = new Order();
        order.setShipChannel(shipBo.getShipChannel());
        order.setShipSn(shipBo.getShipSn());
        order.setOrderStatus((short) 301);
        order.setShipTime(new Date());
        order.setUpdateTime(new Date());

        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(shipBo.getOrderId());
        orderMapper.updateByExampleSelective(order, orderExample);

        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    @Override
    public BaseRespVo refund(RefundBo refundBo) {
        Order order = new Order();
        order.setOrderStatus((short) 203);
        order.setAftersaleStatus((short) 3);
        order.setRefundAmount(refundBo.getRefundMoney());
        order.setRefundTime(new Date());
        order.setUpdateTime(new Date());

        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(refundBo.getOrderId());
        orderMapper.updateByExampleSelective(order, orderExample);

        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    @Override
    public BaseRespVo channel() {
        List<ChannelVo> list = orderMapper.selectAllChannel();
        BaseRespVo<List<ChannelVo>> listBaseRespVo = new BaseRespVo<>();
        listBaseRespVo.setErrmsg("成功");
        listBaseRespVo.setErrno(0);
        listBaseRespVo.setData(list);
        return listBaseRespVo;
    }

    @Override
    public BaseRespVo reply(@RequestBody CommentBo commentBo) {
        String adminContent = commentMapper.selectByPrimaryKey(commentBo.getCommentId()).getAdminContent();
        if (adminContent != null && !"".equals(adminContent)) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrmsg("订单商品已回复！");
            baseRespVo.setErrno(622);
            return baseRespVo;
        }
        Comment comment = new Comment();
        comment.setAdminContent(commentBo.getContent());
        comment.setUpdateTime(new Date());
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIdEqualTo(commentBo.getCommentId());
        commentMapper.updateByExampleSelective(comment, commentExample);
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    @Override
    public SubmitVo submit(CreateOrderBo createOrderBo) {
        SubmitVo submitVo = new SubmitVo();
        MarketOrder marketOrder=new MarketOrder();
        Short comments=0;
        BigDecimal price =new BigDecimal(0);
        List<MarketOrderGoods> marketOrderGoods=new ArrayList<>();
        Integer userId = GetLogedUser.getUserId();
        marketOrder.setUserId(userId);

        List<MarketCart> marketCarts = marketCartMapper.selectByExample(new MarketCartExample());
        for (MarketCart marketCart : marketCarts) {
            if (userId.equals(marketCart.getUserId())&&marketCart.getChecked()&&!marketCart.getDeleted()){
                marketOrderGoods.add(new MarketOrderGoods(null,null,marketCart.getGoodsId(),marketCart.getGoodsSn(),marketCart.getGoodsName(),marketCart.getProductId(),
                        marketCart.getPrice(),marketCart.getNumber(),marketCart.getSpecifications(),0,
                        marketCart.getPicUrl(),birthday,birthday,marketCart.getDeleted()));
                MarketGoodsProduct marketGoodsProduct = marketGoodsProductMapper.selectByPrimaryKey(marketCart.getProductId());
                int  number = (int )marketCart.getNumber()-Integer.parseInt(marketGoodsProduct.getNumber());
                marketGoodsProduct.setNumber( String.valueOf(number));
                marketGoodsProductMapper.updateByPrimaryKey(marketGoodsProduct);
                marketCart.setDeleted(true);
                  //删除占时更新时间
                marketCart.setUpdateTime(birthday);
                marketCartMapper.updateByPrimaryKey(marketCart);
               price = price.add( marketCart.getPrice().multiply(new BigDecimal(marketCart.getNumber())));
                comments++;
            }
        }
        marketOrder.setComments(comments);
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(createOrderBo.getAddressId());
        marketOrder.setConsignee(marketAddress.getName());
        marketOrder.setMobile(marketAddress.getTel());
        marketOrder.setAddress(marketAddress.getProvince()+marketAddress.getCity()+marketAddress.getCounty()+marketAddress.getAddressDetail());

//优惠券价格改为失效
        if (createOrderBo.getCouponId()==0){marketOrder.setCouponPrice(new BigDecimal(0));}
    else {
            List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(new MarketCouponUserExample());
            for (MarketCouponUser marketCoupon : marketCouponUsers) {
                if(marketCoupon.getCouponId().equals(createOrderBo.getCouponId())&&marketCoupon.getStatus()==0&&marketCoupon.getUserId().equals(userId)&&!marketCoupon.getDeleted()){
                marketOrder.setCouponPrice( marketCouponMapper.selectByPrimaryKey(createOrderBo.getCouponId()).getDiscount());
                marketCoupon.setDeleted(true);
                marketCoupon.setStatus((short)1);
                    marketCoupon.setUpdateTime(birthday);
                marketCouponUserMapper.updateByPrimaryKey(marketCoupon);
                 break;
                }

            }
          }
        //团购价格暂时W为0
//      MarketGrouponRules marketGrouponRules = marketGrouponRulesMapper.selectByPrimaryKey(createOrderBo.getGrouponRulesId());
//       if(marketGrouponRules.getStatus()==0){ marketOrder.setGrouponPrice(marketGrouponRules.getDiscount());}

        marketOrder.setGrouponPrice(new BigDecimal(0));
        marketOrder.setGoodsPrice(price);
        MarketSystem marketSystem = marketSystemMapper.selectByPrimaryKey(8);
        marketOrder.setIntegralPrice( new BigDecimal(0));//  不知道在哪里用户积分减免
        if (price.compareTo(new BigDecimal(marketSystemMapper.selectByPrimaryKey(6).getKeyValue()))>-1){
            marketOrder.setFreightPrice(new BigDecimal(0));
        }else {  marketOrder.setFreightPrice(new BigDecimal(marketSystem.getKeyValue()));}
        marketOrder.setOrderPrice(marketOrder.getGoodsPrice().add(marketOrder.getFreightPrice()).subtract(marketOrder.getCouponPrice()));
        marketOrder.setActualPrice(marketOrder.getOrderPrice().subtract(marketOrder.getIntegralPrice()));
        Date date = new Date();//获取当前的日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String str = df.format(date);
        marketOrder.setOrderSn(str.substring(0, 4) + str.substring(5, 7) + str.substring(8, 10)+str.substring(11,13)+str.substring(14, 16)+str.substring(17, 19));
        marketOrder.setAftersaleStatus((short)0);
        marketOrder.setOrderStatus((short)101);
        marketOrder.setAddTime(birthday);
        marketOrder.setUpdateTime(birthday);

       marketOrder.setMessage(createOrderBo.getMessage());
        marketOrder.setDeleted(false);
        marketOrderMapper.insert(marketOrder);

        for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
            marketOrderGood.setOrderId(marketOrder.getId());
            marketOrderGoodsMapper.insert(marketOrderGood);
        }

        //      userCouponId: 2，这个字段还不知道干嘛的，占时先当做更改CouponUser的orderid字段
        marketCouponUserMapper.update(createOrderBo.getUserCouponId(),marketOrder.getId());
        submitVo.setOrderId(marketOrder.getId());
        submitVo.setGrouponLinkId(0);//这是啥玩意那个表出来的东西先不填了
        return submitVo;
    }



    @Override
    public void prepay(IdPo oderId) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(oderId.getId());
        Date date = new Date();//获取当前的日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String str = df.format(date);
        marketOrder.setPayTime(birthday);
        //付款ID乱写的不知道付款表在哪
        marketOrder.setPayId(str.substring(0, 4) + str.substring(5, 7) + str.substring(8, 10)+str.substring(11,13)+str.substring(14, 16)+str.substring(17, 19));
        marketOrder.setOrderStatus((short)201);
        marketOrderMapper.updateByPrimaryKey(marketOrder);
    }

    @Override
    public void cancel(IdPo oderId) {

        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(oderId.getId());
        marketOrder.setEndTime(birthday);
        marketOrder.setOrderStatus((short)102);
        marketOrderMapper.updateByPrimaryKey(marketOrder);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(new MarketOrderGoodsExample());
        for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
            MarketGoodsProduct marketGoodsProduct = marketGoodsProductMapper.selectByPrimaryKey(marketOrderGood.getProductId());
            int  number = (int )marketOrderGood.getNumber()+Integer.parseInt(marketGoodsProduct.getNumber());
            marketGoodsProduct.setNumber( String.valueOf(number));
            marketGoodsProductMapper.updateByPrimaryKey(marketGoodsProduct);
            marketOrderGood.setDeleted(true);
            marketOrderGoodsMapper.updateByPrimaryKey(marketOrderGood);
        }

        reCoupon( marketOrder);

    }

    private void reCoupon(MarketOrder marketOrder) {
        Integer userId = GetLogedUser.getUserId();
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        criteria.andDiscountEqualTo(marketOrder.getCouponPrice());
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(example);
        for (MarketCoupon marketCoupon : marketCoupons) {
            MarketCouponUser marketCouponUser = new MarketCouponUser();
            marketCouponUser.setStartTime(marketCoupon.getStartTime());
            marketCouponUser.setEndTime(marketCoupon.getEndTime());
            marketCouponUser.setCouponId(marketCoupon.getId());
            appletCouponService.couponReceive(userId,marketCouponUser);
            break;
        }
    }


    @Override
    public void refund(IdPo oderId) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(oderId.getId());
        marketOrder.setRefundAmount(marketOrder.getActualPrice());
        marketOrder.setOrderStatus((short)202);
        marketOrder.setRefundContent("您的订单即将退款");
        marketOrder.setRefundType("原路返回");
        marketOrder.setRefundTime(birthday);
        marketOrderMapper.updateByPrimaryKey(marketOrder);
        reCoupon( marketOrder);

    }
    @Override
    public void delete(IdPo oderId) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(oderId.getId());
        marketOrder.setDeleted(true);
        marketOrderMapper.updateByPrimaryKey(marketOrder);
    }



    @Override
    public void confirm(IdPo oderId) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(oderId.getId());
        marketOrder.setRefundAmount(marketOrder.getActualPrice());
        marketOrder.setOrderStatus((short)401);
        marketOrder.setConfirmTime(birthday);
        marketOrderMapper.updateByPrimaryKey(marketOrder);
    }
  static  private Integer commentid=null;
    static  private Integer orderIds=null;
    @Override
    public void comment(MarketComment marketComment) {

        MarketComment marketComment1 = marketCommentMapper.selectByPrimaryKey(commentid);
        marketComment1.setUpdateTime(birthday);
        marketComment1.setContent(  marketComment.getContent());
        marketComment1.setStar( marketComment.getStar());
        marketComment1.setPicUrls( marketComment.getPicUrls());
        marketComment1.setHasPicture( marketComment.getHasPicture());
        marketComment1.setDeleted(false);
        marketCommentMapper.updateByPrimaryKey(marketComment1);
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderIds);
        int  comments = (int )marketOrder.getComments();
        comments=comments-1;
       if(comments>=0){  marketOrder.setComments((short) comments);} ;
        marketOrderMapper.updateByPrimaryKey(marketOrder);

    }

    @Override
    public void goods(Integer orderId, Integer goodsId) {
        orderIds =  orderId;
        MarketComment marketComment = new MarketComment();
        marketComment.setValueId(goodsId);
        marketComment.setType((byte)0);
        marketComment.setAddTime(birthday);
        Integer userId = GetLogedUser.getUserId();
        marketComment.setUserId(userId);
        marketCommentMapper.insert(marketComment);

        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(new MarketOrderGoodsExample());
        for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
          if (marketOrderGood.getGoodsId().equals(goodsId)&&marketOrderGood.getOrderId().equals(orderId)) {
              commentid = marketComment.getId();
              marketOrderGood.setComment(commentid);
              marketOrderGoodsMapper.updateByPrimaryKey(marketOrderGood);
          }
        }

    }


}

